package com.ss.web.tagmark.dao;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.ss.web.tagmark.entity.Article;

/**
 * 数据库方面的操作
 *
 * @author taofucheng
 *
 */
public interface ArticleDao extends BaseRepository<Article, Long> {

	default Page<Article> findByPage(Article article, Pageable page, EntityManager em) {
		String sql = "SELECT * FROM  `article` u WHERE 1=1 ";
		Map<String, Object> params = new HashMap<>();
		if (StringUtils.isNotBlank(article.getTitle())) {
			sql += " and MATCH(`title`) AGAINST (CONCAT('*',:title,'*') IN BOOLEAN MODE)";
			params.put("title", article.getTitle());
		}
		if (StringUtils.isNotBlank(article.getAuthor())) {
			sql += " and MATCH(`author`) AGAINST (CONCAT('*',:author,'*') IN BOOLEAN MODE)";
			params.put("author", article.getAuthor());
		}
		if (StringUtils.isNotBlank(article.getKeyword())) {
			sql += " and MATCH(`keyword`) AGAINST (CONCAT('*',:keyword,'*') IN BOOLEAN MODE)";
			params.put("keyword", article.getKeyword());
		}
		if (StringUtils.isNotBlank(article.getContent())) {
			sql += " and MATCH(`content`) AGAINST (CONCAT('*',:content,'*') IN BOOLEAN MODE)";
			params.put("content", article.getContent());
		}
		return doQuery(page, sql, Article.class, params, em);
	}

}
